/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.alien.admin.modules.deploy.rest;


import com.alien.admin.annotation.Log;
import com.alien.admin.modules.deploy.domain.DpImageBuildTpl;
import com.alien.admin.modules.deploy.service.DpImageBuildTplService;
import com.alien.admin.modules.deploy.service.dto.DpImageBuildTplDto;
import com.alien.admin.modules.deploy.service.dto.DpImageBuildTplQueryCriteria;
import com.alien.admin.modules.deploy.service.mapstruct.DpImageBuildTplMapper;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;

import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
 * @author zengzhifang
 * @website https://el-admin.vip
 * @date 2021-09-17
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "镜像构建模板表管理")
@RequestMapping("/api/dpImageBuildTpl")
public class DpImageBuildTplController {

    private final DpImageBuildTplService dpImageBuildTplService;

    private final DpImageBuildTplMapper dpImageBuildTplMapper;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('dpServer:list')")
    public void download(HttpServletResponse response, DpImageBuildTplQueryCriteria criteria) throws IOException {
        dpImageBuildTplService.download(dpImageBuildTplService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询镜像构建模板表")
    @ApiOperation("查询镜像构建模板表")
    @PreAuthorize("@el.check('dpServer:list')")
    public ResponseEntity<Object> query(DpImageBuildTplQueryCriteria criteria, Pageable pageable) {
        return new ResponseEntity<>(dpImageBuildTplService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @GetMapping(value = "getByProjectIdAndEnv")
    @Log("查询某个服务某个环境的镜像构建模板表")
    @ApiOperation("查询镜像构建模板表")
    @PreAuthorize("@el.check('dpServer:list')")
    public ResponseEntity<Object> getByProjectIdAndEnv(DpImageBuildTplQueryCriteria criteria) {
        return new ResponseEntity<>(dpImageBuildTplService.getByProjectIdAndEnv(criteria), HttpStatus.OK);
    }


    @PostMapping
    @Log("新增镜像构建模板表")
    @ApiOperation("新增镜像构建模板表")
    @PreAuthorize("@el.check('dpServer:add')")
    public ResponseEntity<Object> create(@Validated @RequestBody DpImageBuildTpl resources) {
        return new ResponseEntity<>(dpImageBuildTplService.create(resources), HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改镜像构建模板表")
    @ApiOperation("修改镜像构建模板表")
    @PreAuthorize("@el.check('dpServer:edit')")
    public ResponseEntity<Object> update(@Validated @RequestBody DpImageBuildTpl resources) {
        dpImageBuildTplService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "updateJobParameterOnly")
    @Log("修改镜像构建模板的job字段")
    @ApiOperation("修改镜像构建模板的job字段")
    @PreAuthorize("@el.check('dpServer:edit')")
    public ResponseEntity<Object> updateJobParameterOnly(@RequestBody DpImageBuildTpl resources) {
        DpImageBuildTplDto dto = dpImageBuildTplService.findById(resources.getId());
        DpImageBuildTpl entityFromDb = dpImageBuildTplMapper.toEntity(dto);
        entityFromDb.setContent(resources.getContent());
        dpImageBuildTplService.update(entityFromDb);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @PutMapping(value = "updateParamFormOnly")
    @Log("修改镜像构建模板的ParamForm字段")
    @ApiOperation("修改镜像构建模板的ParamForm字段")
    @PreAuthorize("@el.check('dpServer:edit')")
    public ResponseEntity<Object> updateParamFormOnly(@RequestBody DpImageBuildTpl resources) {
        DpImageBuildTplDto dto = dpImageBuildTplService.findById(resources.getId());
        DpImageBuildTpl entityFromDb = dpImageBuildTplMapper.toEntity(dto);
        entityFromDb.setParamForm(resources.getParamForm());
        dpImageBuildTplService.update(entityFromDb);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @Log("删除镜像构建模板表")
    @ApiOperation("删除镜像构建模板表")
    @PreAuthorize("@el.check('dpServer:del')")
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Integer[] ids) {
        dpImageBuildTplService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}